package com.kaili.basic.controller.web.v1;


import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.ReceivableHandleAddDto;
import com.kaili.basic.pojo.dto.ReceivableHandleQueryDto;
import com.kaili.basic.pojo.dto.ReceivableHandleUpdateDto;
import com.kaili.basic.pojo.entity.HandleExport;
import com.kaili.basic.pojo.entity.ReceivableExport;
import com.kaili.basic.pojo.entity.ReceivableHandle;
import com.kaili.basic.service.ReceivableHandleService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.util.LoginDetailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 应收付管理 前端控制器
 * </p>
 *
 * @author xxx
 * @since 2019-03-07
 */
@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "应收付管理", description = "应收付管理相关API")
public class ReceivableHandleController {
    @Autowired
    ReceivableHandleService receivableHandleService;

    @PostMapping(value = "/receivableHandles/addReceivableHandle")
    @ApiOperation(value = "新增应收付",notes = "新增应收付")
    public RestStatus addReceivableHandle(@RequestBody ReceivableHandleAddDto receivableHandleAddDto){
        ReceivableHandle receivableHandle = new ReceivableHandle();
        BeanUtils.copyProperties(receivableHandleAddDto, receivableHandle);
        /*Long id = receivableHandleService.getMaxReceivableHandleId();
        id = id + 1L;
        receivableHandle.setSerialNumber(StringUtils.leftPad(id.toString(), 7, "0"));*/
        receivableHandle.setId(Long.parseLong(receivableHandle.getSerialNumber()));
        receivableHandle.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        receivableHandleService.addReceivableHandle(receivableHandle);
        return CommonStatus.ADD_OK;
    }

    @PutMapping(value = "/receivableHandles/updateReceivableHandle")
    @ApiOperation(value = "修改应收付",notes = "修改应收付")
    public RestStatus updateReceivableHandle(@RequestBody ReceivableHandleUpdateDto receivableHandleUpdateDto){
        ReceivableHandle receivableHandle = new ReceivableHandle();
        BeanUtils.copyProperties(receivableHandleUpdateDto, receivableHandle);
        receivableHandle.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        receivableHandleService.updateReceivableHandle(receivableHandle);
        return CommonStatus.UPDATE_OK;
    }

    @DeleteMapping("/receivableHandles/{ids}/deleteReceivableHandles")
    @ApiOperation(value = "删除应收付", notes = "删除应收付")
    public RestStatus deleteReceivableHandles(@PathVariable Long[] ids) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("modifyBy",LoginDetailUtils.getCurrentEmployeeId().longValue());
        map.put("ids",ids);
        receivableHandleService.deleteReceivableHandles(map);
        return CommonStatus.DELETE_OK;
    }

    @GetMapping(value = "/receivableHandles/queryReceivableHandle")
    @ApiOperation(value = "条件查询应收付信息",notes = "条件查询应收付信息")
    public Page<ReceivableHandle> queryReceivableHandle(@ModelAttribute ReceivableHandleQueryDto receivableHandleQueryDto) {
        return receivableHandleService.queryReceivableHandle(new Page<ReceivableHandle>(receivableHandleQueryDto.getPage(),receivableHandleQueryDto.getPageSize()),receivableHandleQueryDto);
    }

    @GetMapping(value = "/receivableHandles/{id}/queryReceivableHandleById")
    @ApiOperation(value = "id查询应收付信息",notes = "id查询应收付信息")
    public ReceivableHandle queryReceivableHandleById(@PathVariable("id") Long id) {
        return receivableHandleService.queryReceivableHandleById(id);
    }

    @PutMapping(value = "/receivableHandles/{id}/approve")
    @ApiOperation(value = "审核通过",notes = "审核通过")
    public RestStatus approve(@PathVariable("id") Long id) {
        ReceivableHandle receivableHandle = new ReceivableHandle();
        receivableHandle.setId(id);
        receivableHandle.setStatus(2);
        receivableHandle.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        receivableHandleService.updateReceivableHandle(receivableHandle);
        return CommonStatus.ORDER_WAVE_SUCCESS;
    }

    @PutMapping(value = "/receivableHandles/{id}/rejected")
    @ApiOperation(value = "审核不通过",notes = "审核不通过")
    public RestStatus rejected(@PathVariable("id") Long id) {
        ReceivableHandle receivableHandle = new ReceivableHandle();
        receivableHandle.setId(id);
        receivableHandle.setStatus(3);
        receivableHandle.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        receivableHandleService.updateReceivableHandle(receivableHandle);
        return CommonStatus.ORDER_WAVE_SUCCESS;
    }

    @GetMapping(value = "/receivableHandles/getSerialNumber")
    @ApiOperation(value = "获取流水号",notes = "获取流水号")
    public String getSerialNumber() {
        Long id = receivableHandleService.getMaxReceivableHandleId();
        id = id + 1L;
        return StringUtils.leftPad(id.toString(), 7, "0");
    }

    @GetMapping("/receivableHandles/queryReceivableExport")
    @ApiOperation(value = "导出应收信息")
    public void queryReceivableExport(HttpServletResponse response, @ModelAttribute ReceivableHandleQueryDto receivableHandleQueryDto) throws RestException {
        List<ReceivableExport> list = receivableHandleService.queryReceivableExport(receivableHandleQueryDto);
        ExcelUtil.exportExcel(list,"应收信息","sheet1", ReceivableExport.class, UUID.randomUUID().toString()+".xls",response);
    }

    @GetMapping("/receivableHandles/queryHandleExport")
    @ApiOperation(value = "导出应付信息")
    public void queryHandleExport(HttpServletResponse response, @ModelAttribute ReceivableHandleQueryDto receivableHandleQueryDto) throws RestException {
        List<HandleExport> list = receivableHandleService.queryHandleExport(receivableHandleQueryDto);
        ExcelUtil.exportExcel(list,"应付信息","sheet1", HandleExport.class, UUID.randomUUID().toString()+".xls",response);
    }
}

